package com.xm.xmapp.fragment;

import java.util.ArrayList;

import android.view.View;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.VolleyError;
import com.xm.xmapp.R;
import com.xm.xmapp.adapter.YHQAdapter;
import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.customview.XListView;
import com.xm.xmapp.customview.XListView.IXListViewListener;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Get_user_coupons_listRequest;
import com.xm.xmapp.response.bean.BaseListResponse;
import com.xm.xmapp.response.bean.ResponseUserCouponsList;
import com.xm.xmapp.tools.Tools;

public class YHQFragment extends BaseFragment {

	private XListView xListView;
	private RelativeLayout rel;
	private Get_user_coupons_listRequest requestBean = new Get_user_coupons_listRequest();
	private int type;
	private int page;
	private int flag;
	private YHQAdapter adapter;
	private ResponseUserCouponsList bean;
	private ArrayList<ResponseUserCouponsList> list = new ArrayList<ResponseUserCouponsList>();
	private String money;

	public void setData(int type, int flag, ResponseUserCouponsList bean, String money) {
		this.type = type;
		this.flag = flag;
		this.bean = bean;
		this.money = money;
	}

	@Override
	public int setContentView() {
		return R.layout.fragment_yhq;
	}

	public YHQFragment() {
		super();
	}

	@Override
	public void initView() {
		xListView = (XListView) findViewById(R.id.xListView);
		rel = (RelativeLayout) findViewById(R.id.rel);
	}

	@Override
	public void initData() {
		page = 1;
		requestBean.pagesize = CodeConstant.pageSize;
		requestBean.type = type;
		requestBean.money = money;
		promptDialog.show();
		xListView.setPullRefreshEnable(true);
		xListView.setPullLoadEnable(true);
		adapter = new YHQAdapter(type, bean, flag, list, getActivity(), R.layout.item_fragment_yhq);
		xListView.setAdapter(adapter);
		xListView.setVisibility(View.GONE);
		rel.setVisibility(View.VISIBLE);
		getData();
	}

	private void getData() {

		requestBean.page = page;
		MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {

				xListView.stopRefresh();
				xListView.stopLoadMore();
				promptDialog.dismiss();
				BaseListResponse<ResponseUserCouponsList> base = JSON.parseObject(response,
						new TypeReference<BaseListResponse<ResponseUserCouponsList>>() {
						});
				if (page == 1)
					list.clear();
				list.addAll(base.list);
				if (base.list == null || base.list.size() == 0) {
					xListView.setPullLoadEnable(false);
					Tools.showMessage("没有更多数据");
				}
				if (list.size() == 0) {
					xListView.setVisibility(View.GONE);
					rel.setVisibility(View.VISIBLE);
				} else {
					xListView.setVisibility(View.VISIBLE);
					rel.setVisibility(View.GONE);
				}
				adapter.notifyDataSetChanged();
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {

				xListView.stopRefresh();
				xListView.stopLoadMore();
				promptDialog.dismiss();
			}
		});

	}

	@Override
	public void initEvent() {

		xListView.setXListViewListener(new IXListViewListener() {

			@Override
			public void onRefresh() {
				xListView.setPullLoadEnable(true);
				page = 1;
				getData();
			}

			@Override
			public void onLoadMore() {

				page++;
				getData();
			}
		});
	}

}
